//
//  XJVisitorView.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/19.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit
import SnapKit
class XJVisitorView: UIView {
    
    //定义一个闭包去传递中间两个button的点击
    
    var closure:(()->())?
    
    //自定义未登录时候显示的view
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupVisitorViewInfo(imgName: String?, title: String?) {
        
        if let img = imgName, let tit = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            feedImageView.hidden = true
        }else{
            //代表首页
            setupAnim()
        }
    }
    
    func setupAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    func setupUI() {
        //设置未登录状态界面
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        //添加控件
        addSubview(feedImageView)
        addSubview(maskImgeView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(regButton)
        //设置约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        maskImgeView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(36)
            make.width.equalTo(230)
            make.top.equalTo(maskImgeView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.height.equalTo(36)
            make.width.equalTo(100)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        regButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.height.width.equalTo(loginButton)
            make.trailing.equalTo(messageLabel)
        }
        
    }
    
    //中间button的监听事件
    
    func clickLoginButton(){
        closure?()
    }
    
    //懒加载控件
    //首页的圆圈
    private lazy var feedImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var maskImgeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var messageLabel: UILabel = {
       
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        label.textAlignment = .Center
        return label
    }()
    private lazy var loginButton: UIButton = {
       
        let button = UIButton()
//        button.titleLabel?.text = "登录"
        button.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return button
    }()
    
    private lazy var regButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return button
    }()
}

